package com.slkj.shilixiaoyuanapp.ui.parent.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.slkj.shilixiaoyuanapp.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：何磊 on 2018/10/9 13:27
 * 描述：
 */
public class ExerciseDetailAdapter extends RecyclerView.Adapter<ExerciseDetailAdapter.MyViewHolder> {
    private Context context;
    private List<String> list;
    private List<String> proList=new ArrayList<>();
    private List<String> longList=new ArrayList<>();
    String pro;
    String longTime;

    public ExerciseDetailAdapter(Context context,List<String> list){
        this.context=context;
        this.list=list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_exercise, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position)
    {
        holder.tv_exercise_num.setText("锻炼明细"+(position+1));
        if (position!=0){
            holder.tv_delete.setVisibility(View.VISIBLE);
            holder.tv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    list.remove(position);
                    notifyDataSetChanged();
                    if (proList.size()==(position+1)){
                        proList.remove(position);
                    }
                    if (longList.size()==(position+1)){
                        longList.remove(position);
                    }
                }
            });
        }
        holder.et_exercise_pro.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                pro=s.toString();
            }
        });
        holder.et_exercise_long.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                longTime=s.toString();
            }
        });
        if (pro!=null&&longTime!=null) {
            proList.add(pro);
            longList.add(longTime);
        }
        pro=null;
        longTime=null;
        onTextChangeListener.onTextChange(proList,longList);
    }

    @Override
    public int getItemCount()
    {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder
    {
        TextView tv_exercise_num;
        TextView tv_delete;
        EditText et_exercise_pro;
        EditText et_exercise_long;
        public MyViewHolder(View view)
        {
            super(view);
            tv_exercise_num=view.findViewById(R.id.tv_exercise_num);
            tv_delete=view.findViewById(R.id.tv_delete);
            et_exercise_pro=view.findViewById(R.id.et_exercise_pro);
            et_exercise_long=view.findViewById(R.id.et_exercise_long);
        }
    }
    private OnTextChangeListener onTextChangeListener=null;
    public interface OnTextChangeListener{
        void onTextChange(List<String> proList,List<String> longList);
    }

    public void setOnTextChangeListener(OnTextChangeListener listener){
        this.onTextChangeListener=listener;
    }
}
